﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SysRouteLinq.Forms
{
    public partial class ConsultaParada : Form
    {
        private SysRouteDataContext dc;
        private bool result = false;

        public ConsultaParada()
        {
            InitializeComponent();
            dc = new SysRouteDataContext();
        }

        private void ConsultaParada_Shown(object sender, EventArgs e)
        {
            fillComboBox();
        }

        private void fillComboBox()
        {
            IEnumerable<Parada> p = from f in dc.Paradas
                                    orderby
                                        f.nome
                                    select f;
            comboBox1.DataSource = p.ToList();
            comboBox1.DisplayMember = "nome";
            comboBox1.ValueMember = "id_parada";

            Parada p2 = (from f in dc.Paradas
                         where f == comboBox1.SelectedItem
                         select f).Single();

            textBox2.Text = p2.referencia;

            result = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Parada r = (from f in dc.Paradas
                         where
                         f == comboBox1.SelectedItem
                         select f).Single();
           
            IEnumerable<LinhaParada> p = (from f2 in dc.LinhaParadas
                                    where
                                       f2.id_parada == r.id_parada
                                    select f2);

            foreach (LinhaParada i in p)
            {
                dc.LinhaParadas.DeleteOnSubmit(i);
            }
           
            dc.Paradas.DeleteOnSubmit(r);
            dc.SubmitChanges();
            
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (result == true)
            {
                Parada r = (from f in dc.Paradas
                            where f == comboBox1.SelectedItem
                            select f).Single();

                textBox2.Text = r.referencia;
            }
            else { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Parada p = (from f in dc.Paradas
                         where
                         f == comboBox1.SelectedItem
                         select f).Single();

            EditParada form = new EditParada(p.id_parada);
            if (form.ShowDialog() == DialogResult.OK)
            {
                dc = new SysRouteDataContext();
                fillComboBox();
            }
        }
    }
}
